For standard HTML attribute information about the <BODY>
element, see the <BODY>
topic.
The <BODY>
element currently supports four scripting events. Two are currently only supported by Netscape.
OnBlur
This event handler can be used to execute a script when the window that contains the document that contains the script loses the users focus. This will fire whatever routines are scripted in the event when the user moves the focus to any other window on their system, so should be used with care. However, it can be very useful for example, for tracking the users focus if your web site (or application) uses multiple windows. For example :
<BODY OnBlur="comeback()">
. . .
<SCRIPT LANGUAGE="JavScript">
<!--
function comeback()
{
alert ("Y'all come back now")
}
//-->
</SCRIPT>
would display the message 'Y'all come back now' whenever the user moves the focus away from the browser window.
NOTE : Currently, only Netscape supports this event.
OnError
This event handler can be used to execute script events if there is either a document loading error, or a navigator error. As such, it can be useful for providing valuable debugging information when developing JavaScripts.
OnFocus
The opposite of the above event, this event handler can be used to execute a script when the window that contains the document that contains the script gets the users focus. As above, this will fire whatever routines are scripted in the event when the user moves the focus to the browser window, so should be used with care. For example :
<BODY OnFocus="cameback()">
. . .
<SCRIPT LANGUAGE="JavScript">
<!--
function cameback()
{
alert ("Welcome back")
}
//-->
</SCRIPT>
would display the message 'Welcome back' whenever the user returns the focus to the browser window.
NOTE : Currently, only Netscape supports this event.
OnLoad
This event handler can be used to execute a script when the document has finished loading in the browser window. Commonly, you may see this event being used to start status bar 'ticker tape' scrolling messages. Another example is the creation of a JavaScript scrolling marquee type text box. For example, using the following OnLoad
event causes the marquee text box below to start as soon as the document loads. (The startMarquee()
function is written into the script.
<BODY OnLoad="startMarquee()">
OnUnLoad
The opposite of the above event, this event handler can be used to execute a script function when the user leaves the current document. Re-using the above example, we can add the OnUnLoad
event to the <BODY>
element, to stop the 'marquee' before the document is left.
<BODY OnLoad="startMarquee()" OnUnLoad="stopMarquee()">